//
//  ViewController.swift
//  SwiftClockDemo
//
//  Created by gwj on 10/14/15.
//  Copyright (c) 2015 gwj. All rights reserved.
//

import UIKit

    class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{

    
    @IBOutlet var alertTableView: UITableView!
    
    @IBOutlet var cancelBtn: UIBarButtonItem!
    
    @IBOutlet var saveBtn: UIBarButtonItem!
    
        var isCanEditing : Bool = false
        var tableviewDataArray :NSMutableArray!//类型加！（！是隐式解析可选，？是可选），写了！， 就可以不用初始化Array： var tableviewDataArray :NSArray = NSArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        alertTableView.delegate = self
        alertTableView.dataSource = self
        tableviewDataArray = ["1","2"]
//        cancelBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action:"didTappedCancelBtn")
//        
//        saveBtn = UIBarButtonItem (barButtonSystemItem: UIBarButtonSystemItem.Action, target: self, action: "didTappedSaveBtn")
        // Do any additional setup after loading the view, typically from a nib.
    }
        
    //MARK: editignButtonMethord
        @IBAction func didTapepdEditingButton(sender: UIButton) {
            
//            self.alertTableView .setEditing(true, animated: true)
            isCanEditing = true
        }
        
    // MARK: tableViewDataSource
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("AlertTableViewCell", forIndexPath: indexPath)as! AlertTableViewCell
            return cell
        }
        
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return tableviewDataArray.count
        }
    
    // MARK: tableViewDelegate 
        func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
            return isCanEditing
        }
        
        func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
            return UITableViewCellEditingStyle.Delete
        }
        
        func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
            if editingStyle == UITableViewCellEditingStyle.Delete {
            tableView.deleteRowsAtIndexPaths(tableviewDataArray.objectAtIndex(indexPath.row) as! [NSIndexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
            }
            
        }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

